package com.his.struts.action;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.type.TypeDao;
import com.his.struts.dao.type.TypeImp;
import com.his.struts.dao.users.UsersDao;
import com.his.struts.dao.users.UsersImp;
import com.his.struts.form.DiseaseForm;
import com.his.struts.form.InHisDiseaseForm;
import com.his.struts.form.UserForm;
import com.his.struts.form.ZydiseaseForm;
import com.his.struts.util.PageUtil;
/**
 * 病例功能类
 * @author liuyan
 *
 */
public class DiseaseAction extends DispatchAction {
	UsersDao dao = new UsersImp();

	/**
	 * 查询 所有的门诊病历
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward allDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		/*
		 * 后台访问
		 */
		String id = request.getParameter("uid");
		if ("".equals(id)) {
			UserForm uf = (UserForm) session.getAttribute("userinfo");
			int mid = 0;
			if(uf != null){
				mid = uf.getUid();
			}
			if (mid == 0) {
				Integer intid = (Integer) session.getAttribute("admin_uid");
				id = intid.toString();
			} else {
				id = mid + "";
			}
		}
		String admin = request.getParameter("admin");
		if(admin == null){
			admin = (String)request.getAttribute("admin");
		}
		int uid = 0;
		if (admin != null) {
			uid = Integer.parseInt(id);
			session.setAttribute("admin_uid", uid);
		} else {
			String uname = (String) session.getAttribute("username");
			uid = dao.getuid(uname);
		}

		String sql = "casehis where uid = " + uid;

		ResultSet rs = PageUtil.getPage(sql, 10, request);

		// 如果数据不为空
		if (rs != null) {
			// 调用DAO 渠道所有病例
			List<DiseaseForm> diseaselist = dao.getBean(rs);
			// 数据放到request中
			request.setAttribute("diseaselist", diseaselist);
		}

		request.setAttribute("uid", uid);
		// 通过admin判断是否为后台
		if (admin != null) {
			return mapping.findForward("admin_disease");
		} else {
			return mapping.findForward("successall");
		}

	}

	/**
	 * Method execute 查询 并显示一个门诊病历的信息
	 * 
	 * @param mapping
	 *            配置文件
	 * @param form
	 *            表单数据
	 * @param request
	 *            网页请求
	 * @param response
	 *            响应
	 * @return ActionForward 跳转到查看问题页
	 */
	public ActionForward showDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int caseid = Integer.parseInt(request.getParameter("mycaseid"));
		// 判断是否为后台访问
		String admin = request.getParameter("admin");
		// 通过caseid取一个病历的信息
		DiseaseForm disease = dao.oneDisease(caseid);
		// 数据放到request中
		request.setAttribute("mydisease", disease);
		if (admin != null) {
			return mapping.findForward("admin_bingli");
		} else {
			return mapping.findForward("successshow");
		}

	}

	/**
	 * Method execute 添加一个门诊病历
	 * 
	 * @param mapping
	 *            配置文件
	 * @param form
	 *            表单数据
	 * @param request
	 *            网页请求
	 * @param response
	 *            响应
	 * @return ActionForward 跳转到查看问题页 addDisease addDisease
	 */

	public ActionForward addDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int uid  = Integer.parseInt(request.getParameter("mzuid"));
		DiseaseForm df = (DiseaseForm) form;
		TypeDao td = new TypeImp();
		String facname = df.getFacname();
		int kid = Integer.parseInt(facname);
		df.setFacname(td.getKeshiById(kid));
		df.setUid(uid);
		boolean ok = dao.addDisease(df);
		if(ok){
			request.setAttribute("addok", "添加成功！！！！");
			request.setAttribute("message", "添加成功");
			return mapping.findForward("successadd");
		}else{
			return mapping.findForward("failadd");
		}
	}

	/**
	 * Method execute 所有住院病历
	 * 
	 * @param mapping
	 *            配置文件
	 * @param form
	 *            表单数据
	 * @param request
	 *            网页请求
	 * @param response
	 *            响应
	 * @return ActionForward 跳转到查看问题页
	 */
	public ActionForward allInHisDisease(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HttpSession session = request.getSession();

		/*
		 * 后台访问
		 */
		Integer id = (Integer) session.getAttribute("admin_uid");
		String admin = request.getParameter("admin");
		if (admin == null) {
			admin = (String)request.getAttribute("admin");
		}
		if (admin == null) {
			admin = (String) session.getAttribute("admin");
			session.removeAttribute("admin");
		}
		ActionForward forword = null;
		int uid = 0;
		if (admin != null) {
			uid = id;
		} else {
			UserForm uf = (UserForm) session.getAttribute("userinfo");
			uid = uf.getUid();
		}
		String sql = "inhis where uid = " + uid;
		ResultSet rs = PageUtil.getPage(sql, 10, request);

		// 如果数据不为空
		if (rs != null) {
			// 封装
			List<InHisDiseaseForm> list = dao.getAllInHisDisease(rs);
			// 数据放到request中
			request.setAttribute("allInHisDisease", list);
		}
		if (admin != null) {
			return mapping.findForward("admin_InHisDisease");
		} else {
			forword = mapping.findForward("showAllInHisDisease");
			return forword;
		}

	}

	/**
	 * 我看过到病人
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward lookPatient(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String doctor = (String) request.getSession().getAttribute("ysname");
		if (doctor == null) {
			return mapping.findForward("login");
		}

		String sql = "casehis where recdoctor = '" + doctor + "'";

		ResultSet rs = PageUtil.getPage(sql, 10, request);

		// 如果数据不为空
		if (rs != null) {
			// 封装
			List<DiseaseForm> patient = dao.getBean(rs);
			// 数据放到request中
			request.setAttribute("patient", patient);
		}

		return mapping.findForward("lookPatient");
	}

	/**
	 * 该病人的病例信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward diseaseDetails(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int caseid = Integer.parseInt(request.getParameter("mycaseid"));
		DiseaseForm disease = dao.oneDisease(caseid);
		ActionForward forword = null;
		if (disease != null) {
			request.setAttribute("mydisease", disease);
			forword = mapping.findForward("diseaseDetails");
		} else {
			forword = mapping.findForward("fail");
		}

		return forword;
	}

	/**
	 * 该病人的病例信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DiseaseForm diseaseForm = (DiseaseForm) form;
		int n = dao.updateDisease(diseaseForm);
		String admin = request.getParameter("admin");
		if (admin != null) {
			int uid = diseaseForm.getUid();
			String sql = "casehis where uid = " + uid;
			// 分页
			ResultSet rs = PageUtil.getPage(sql, 10, request);

			// 如果数据不为空
			if (rs != null) {
				// 封装
				List<DiseaseForm> diseaselist = dao.getBean(rs);
				// 数据放到request中
				request.setAttribute("diseaselist", diseaselist);
			}
			request.setAttribute("admin", "ly");
			// 跳后台
			return mapping.findForward("admin_editDisease");
		}
		// 跳前台
		return mapping.findForward("editDisease");
	}

	/**
	 * 删除门诊病例
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward deleteDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int caseid = Integer.parseInt(request.getParameter("mycaseid"));
		dao.deleteDesease(caseid);
		Integer uid = (Integer) request.getSession().getAttribute("admin_uid");

		String sql = "casehis where uid = " + uid;

		ResultSet rs = PageUtil.getPage(sql, 10, request);

		// 如果数据不为空
		if (rs != null) {
			// 封装
			List<DiseaseForm> diseaselist = dao.getBean(rs);
			// 数据放到request中
			request.setAttribute("diseaselist", diseaselist);
		}

		request.setAttribute("admin", "ly");
		request.setAttribute("message", "删除成功");
		return mapping.findForward("admin_showdisease");

	}

	/**
	 * 删除住院病例
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward deleteInHisDisease(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int ihid = Integer.parseInt(request.getParameter("ihid"));
		dao.deleteInHidDesease(ihid);
		request.getSession().setAttribute("admin", "ly");
		request.setAttribute("message", "删除成功");
		return mapping.findForward("admin_showInHisdisease");

	}
	/**
	 * Method execute 添加一个住院病历
	 * 
	 * @param mapping
	 *            配置文件
	 * @param form
	 *            表单数据
	 * @param request
	 *            网页请求
	 * @param response
	 *            响应
	 * @return ActionForward 跳转到查看问题页 addDisease addDisease
	 */
	public ActionForward addInHisDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String inhisuid = request.getParameter("inhisuid");
		int uid  = Integer.parseInt(inhisuid);
		InHisDiseaseForm ihdf = (InHisDiseaseForm) form;
		ihdf.setUid(uid);
		String ywlist = request.getParameter("ywlist");
		String [] yw = ywlist.split(",");
		List list = new ArrayList();
		for (int i = 0; i < yw.length; i++) {
			list.add(yw[i]);
		}
		ihdf.setPrescript(list);
		boolean ok = dao.addInHisDisease(ihdf,1);
		if(ok){
			request.setAttribute("addok", "添加成功！！！！");
			request.setAttribute("message", "添加成功");
			return mapping.findForward("successadd");
		}else{
			return mapping.findForward("failadd");
		}
	}
	
	
	/**
	 * Method execute 显示一个住院病历
	 * 
	 * @param mapping
	 *            配置文件
	 * @param form
	 *            表单数据
	 * @param request
	 *            网页请求
	 * @param response
	 *            响应
	 * @return ActionForward 跳转到查看问题页 
	 */
	public ActionForward showInHisDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String ihid = request.getParameter("ihid");
		if(ihid != null){
			int id  = Integer.parseInt(ihid);
			InHisDiseaseForm ihdf = dao.getInHisDisease(id);
			request.setAttribute("oneInHisDisease", ihdf);
			return mapping.findForward("successShowInHis");
		}else{
			return mapping.findForward("fail");
		}
	}
	
	
	/**
	 * Method execute 编辑一个住院病历
	 * 
	 * @param mapping
	 *            配置文件
	 * @param form
	 *            表单数据
	 * @param request
	 *            网页请求
	 * @param response
	 *            响应
	 * @return ActionForward 跳转到查看问题页 
	 */
	public ActionForward editInHisDisease(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		InHisDiseaseForm ihdf = (InHisDiseaseForm) form;
		String ywlist = request.getParameter("ywlist");
		if(ywlist.startsWith("[")){
			ywlist = ywlist.substring(1);
		}
		if(ywlist.endsWith("]")){
			ywlist = ywlist.substring(0, ywlist.length() - 1);
		}
		String [] yw = ywlist.split(",");
		List list = new ArrayList();
		for (int i = 0; i < yw.length; i++) {
			list.add(yw[i]);
		}
		ihdf.setPrescript(list);
		boolean ok = dao.editInHisDisease(ihdf);
		if(ok){
			request.setAttribute("admin", "ly");
			request.setAttribute("message", "修改成功");
			return mapping.findForward("successedit");
		}else{
			return mapping.findForward("fail");
		}
		
	}
	
	/**
	 * Method execute 跳转添加病例页面
	 * 
	 * @param mapping
	 *            配置文件
	 * @param form
	 *            表单数据
	 * @param request
	 *            网页请求
	 * @param response
	 *            响应
	 * @return ActionForward 跳转到查看问题页 addDisease addDisease
	 */
	public ActionForward addbinli(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("uid");
		if(id != null){
			request.setAttribute("uidadd", id);
			return mapping.findForward("showadd");
		}else{
			return mapping.findForward("fail");
		}
	}
}